package com.springstudy.controller;


import com.fasterxml.jackson.databind.ObjectMapper;
import com.springstudy.pojo.User;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author Tamako
 * @since 2025/1/14 10:37
 */
@Controller
public class ParamController {


    @GetMapping("/param13")
    public String param13(@CookieValue(name = "JSESSIONID", defaultValue = "") String jSessionId) {
        System.out.println(jSessionId);
        return "/index.jsp";
    }

    @GetMapping("/param12")
    public String param12(@RequestHeader Map<String, String> map) {
        System.out.println(map);
        return "/index.jsp";
    }

    @GetMapping("/param11")
    public String param11(@RequestHeader("Accept-Additions") String additions) {
        System.out.println(additions);
        return "/index.jsp";
    }

    //http://localhost:8080/param9
    @PostMapping("/param9")
    public String param8(@RequestBody User user) {
        System.out.println(user);
        return "/index.jsp";
    }

    //http://localhost:8080/param8
    @PostMapping("/param8")
    public String param8(@RequestBody String body) throws Exception {
        ObjectMapper objectMapper = new ObjectMapper();
        User user = objectMapper.readValue(body, User.class);
        System.out.println(user);
        return "/index.jsp";
    }

    //http://localhost:8080/param7
    @PostMapping("/param7")
    public String param7(@RequestBody String body) {
        System.out.println(body);
        return "/index.jsp";
    }

    //http://localhost:8080/param6?username=Tamako&age=25&hobbies=zq&hobbies=pq&hobbies=tq&birthday=2018/11/11&address.city=Beijing&address.area=Chaoyang%20Road
    @GetMapping("/param6")
    public String param6(User user) {
        System.out.println(user);
        return "/index.jsp";
    }

    //http://localhost:8080/param5?username=Tamako&age=25
    @GetMapping("/param5")
    public String param5(@RequestParam Map<String, String> map) {
        map.forEach((k, v) -> System.out.println(k + " " + v));
        return "/index.jsp";
    }

    //http://localhost:8080/param4?hobby=zq&hobby=pq&hoppy=tq
    @GetMapping("/param4")
    public String param4(@RequestParam List<String> hobby) {
        hobby.forEach(System.out::println);
        return "/index.jsp";
    }

    //http://localhost:8080/param3?hobby=zq&hobby=pq&hoppy=tq
    @GetMapping("/param3")
    public String param3(String[] hobby) {
        for (String s : hobby) {
            System.out.println(s);
        }
        return "/index.jsp";
    }

    //http://localhost:8080/param2?username=Tamako&age=25
    @GetMapping("/param2")
    public String param2(@RequestParam(value = "username", required = true, defaultValue = "unknown") String username, @RequestParam("age") int age) {
        System.out.println(username + " " + age);
        return "/index.jsp";
    }

    //http://localhost:8080/param1?username=Tamako&age=25
    @GetMapping("/param1")
    public String param1(String username, int age) {
        System.out.println(username + " " + age);
        return "/index.jsp";
    }
}
